<?php
namespace cms\modules\frontend\controllers;

use Yii;
use cms\controllers\BasicController;
use common\component\tool\Tool;
use common\component\exception\ErrMap;
use common\component\tool\Member;
use common\component\tool\HttpClient;


class AuthController extends BasicController
{

    protected $member;//会员实例
    protected $memberInfo;//登录信息

	public function init()
    {
        parent::init();
        /*请求认证中心,获取用户登录信息*/
        // $this->userInfo = 1;
        // P($this->session->get('memberInfo'));
        // $this->session->get('memberInfo');
        if(isset($this->session->get('memberInfo')->loginname)){
            $this->memberInfo = $this->session->get('memberInfo');
        };

        $this->member = new Member(Yii::$app->params['ac']);
    }

    /*判断是否登录*/
    protected function isLogin()
    {
        if(!empty($this->memberInfo)){
            return true;
        };
        return false;
    }

    /*根据 openid 查询用户信息，并存入session，主要用户小程序过期重复授权的问题*/
    protected function getinfoByOpenid($openid, $comid)
    {
        $data = array(
            'openid' => $openid,
            'comid' => $comid,
        );
        // P($data);
        $res = $this->member->getinfoByOpenid($data, $this->app_info_id);
        // P($res);
        if($res['code'] == 0){
            // P($res['data']);
            $this->session->set('memberInfo', (object)$res['data']);
            $this->memberInfo = $this->session->get('memberInfo');

            // return Tool::showRes([0, '授权成功']);
        }
    }

}
